<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/8
 * Time: 9:59
 */
namespace app\admin\controller;

use app\admin\model\Smstasklog as Smstasklogs;
use app\common\Common;
use think\Request;

class Smstasklog extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $param   = $request->param();
        try {
            if(empty($param)){
               return;
            }elseif (empty($param['name'])&&!empty($param)){
                $list = Smstasklogs::all(function($query) use($param){
                    $where = [];
                    $where1 = [];
                    $data = $param;
                    unset($data['page'],$data['limit'],$data['name'],$data['create_time']);
                    foreach($data as $k=>$v){
                        if($v != 'all'){
                            $where[$k] = $v;
                        }
                    }
                    if(isset($param['create_time']) && $param['create_time'] != 'all'){
                        if($param['create_time'] == 'today'){
                            $from = strtotime(date('Ymd',time()).'00:00:00');
                            $where1['create_time']  = ['BETWEEN',[$from,time()]];
                        }else{
                            $from = strtotime('-'.$param['create_time']);
                            $where1['create_time']  = ['BETWEEN',[$from,time()]];
                        }
                    }
                    $query->where($where)->where($where1)
                        ->limit($param['limit']*($param['page']-1),$param['limit']);
                },'client,smstype,origin');
                if(!empty($list)){
                    collection($list)->append(['sign_content','num'])->toArray();
                }
                $count = Smstasklogs::count();
            }else{
                $where = [];
                $where1 = [];
                $data = $param;
                unset($data['page'],$data['limit'],$data['name'],$data['create_time'],$data['name']);
                foreach($data as $k=>$v){
                    if($v != 'all'){
                        $where[$k] = $v;
                    }
                }
                if(isset($param['create_time']) && $param['create_time'] != 'all'){
                    if($param['create_time'] == 'today'){
                        $from = strtotime(date('Ymd',time()).'00:00:00');
                        $where1['create_time']  = ['BETWEEN',[$from,time()]];
                    }else{
                        $from = strtotime('-'.$param['create_time']);
                        $where1['create_time']  = ['BETWEEN',[$from,time()]];
                    }
                }
                $list = Smstasklogs::hasWhere('client',['name'=>$param['name']])
                    ->with(['client','smstype','origin'])
                    ->where($where)->where($where1)
                    ->limit($param['limit']*($param['page']-1),$param['limit'])
                    ->select();
                $count = Smstasklogs::hasWhere('client',['name'=>$param['name']])
                    ->with(['client','smstype','origin'])
                    ->where($where)->where($where1)->count();
                if(!empty($list)){
                    collection($list)->append(['sign_content','num'])->toArray();
                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        } catch (\Exception $e) {
            // 捕获异常并转发为HTTP异常
            return abort(404, $e->getMessage());
        }
    }
}